using System.Collections.Generic;

namespace ColladaDotNet
{
	public partial class mesh : ColladaXmlNode
	{
		public override IList<ColladaXmlNode> ChildNodes
		{
			get
			{
				var list = new List<ColladaXmlNode>();
				if (source != null)
					foreach (object e in source) if (e is ColladaXmlNode) list.Add((ColladaXmlNode)e);
				if (Items != null)
					foreach (object e in Items) if (e is ColladaXmlNode) list.Add((ColladaXmlNode)e);
				if (extra != null)
					foreach (object e in extra) if (e is ColladaXmlNode) list.Add((ColladaXmlNode)e);
				if (vertices != null)
					list.Add(vertices);
				return list;
			}
		}

		public triangles CreateTriangles(string simbol)
		{
			var node = new triangles();
			node.material = simbol;
			Items = COLLADA.AddToArray(Items, node);
			return node;
		}



		public void AddSource(source source1)
		{
			if (source == null || source.Length == 0)
			{
				source = new source[] { source1 };
				return;
			}
			var g = new source[source.Length + 1];
			source.CopyTo(g, 0);
			g[source.Length] = source1;
			source = g;
		}

		public source CreateXYZSource(string id, double[] array)
		{
			source res = CreateFloatArray(id, array);
			res.technique_common = new sourceTechnique_common()
			{
				accessor = new accessor()
				{
					source = "#" + id + "-array",
					count = (ulong)array.Length / 3,
					stride = 3,
					param = new param[]
					{
						new param(){name = "X",type = "float"},
						new param(){name = "Y",type = "float"},
						new param(){name = "Z",type = "float"},
			        }
				}
			};
			return res;
		}
		public source CreateRGBSource(string id, double[] array)
		{
			source res = CreateFloatArray(id, array);
			res.technique_common = new sourceTechnique_common()
			{
				accessor = new accessor()
				{
					source = "#" + id + "-array",
					count = (ulong)array.Length / 3,
					stride = 3,
					param = new param[]
					{
						new param(){name = "R",type = "float"},
						new param(){name = "G",type = "float"},
						new param(){name = "B",type = "float"},
			        }
				}
			};
			return res;
		}
		private source CreateFloatArray(string id, double[] array)
		{
			var res = new source();
			res.id = id;
			res.Item = new float_array()
			           	{
			           			id = id + "-array",
			           			count = (ulong)array.Length,
			           			Text = array
			           	};
			source= COLLADA.AddToArray(source, res);

			return res;
		}


		public source CreateSTPSource(string id, double[] array)
		{
			source res = CreateFloatArray(id, array);
			res.technique_common = new sourceTechnique_common()
			{
				accessor = new accessor()
				{
					source = "#" + id + "-array",
					count = (ulong)array.Length / 3,
					stride = 3,
					param = new param[]
					{
						new param(){name = "S",type = "float"},
						new param(){name = "T",type = "float"},
						new param(){name = "P",type = "float"},
			        }
				}
			};
			return res;
		}

		public vertices CreatePositionVertices(string id, double[] array)
		{
			var s = CreateXYZSource(id+"-positions",array);
			vertices = new vertices();
			vertices.id = id;
			vertices.input = new InputLocal[] { new InputLocal() { semantic = "POSITION", source = "#"+s.id} };
			return vertices;
		}
	}
}
